package com.jy.datapipeline.export.controller;

import com.jy.datapipeline.common.config.DBMetaDataConfig;
import com.jy.datapipeline.export.entity.DataDictionary;
import com.jy.datapipeline.export.entity.DataSourceInfoModel;
import com.jy.datapipeline.export.entity.ExpRuleModel;
import com.jy.datapipeline.export.service.DataDictionaryService;
import com.jy.datapipeline.export.service.DataSourceInfoService;
import com.jy.datapipeline.export.service.ExpRuleService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
    @RequestMapping(value = "/datasource/")
public class DataSourceInfoController {
    @Autowired
    private DataSourceInfoService dataSourceInfoService;
    @Autowired
    private  DBMetaDataConfig dBMetaDataConfig;
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private ExpRuleService expRuleService;
    @RequestMapping( value = "findByList",method = RequestMethod.GET)
    @ResponseBody
    public Object findDataStoreByList(
        @RequestParam(value = "dataSourceName",required = true) String dataSourceName,
        @RequestParam(value = "iDisplayStart",required = true) Integer iDisplayStart,
        @RequestParam(value = "iDisplayLength",required = true) Integer iDisplayLength
    ){
        Map<String, Object> map=new HashMap<>();
        List<Map<String,Object>> list=this.dataSourceInfoService.findDataSourceInfoByList(dataSourceName,iDisplayStart,iDisplayLength);
        Integer count=this.dataSourceInfoService.findDataSourceInfoByCount(dataSourceName);
        map.put("datalist",list);
        map.put("count", count);
        return map;
    }

    @RequestMapping( value = "add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(
        @RequestParam(value = "dataSourceName",required = true) String dataSourceName,
        @RequestParam(value = "jdbcurl",required = true) String jdbcurl,
        @RequestParam(value = "username",required = true) String username,
        @RequestParam(value = "password",required = true) String password
    ){
        Map<String, Object> result=new HashMap<>();
        try{
            String dataSourceId= "source_"+String.valueOf(System.currentTimeMillis());
            DataSourceInfoModel dsim=new DataSourceInfoModel() ;
            dsim.setDataSourceId(dataSourceId);
            dsim.setDataSourceName(dataSourceName);
            dsim.setUserName(username);
            dsim.setPassword(password);
            dsim.setJdbcUrl(jdbcurl);
            DataSourceInfoModel dsimInfo= this.dataSourceInfoService.findByDataSourceName(dataSourceName);
            if(dsimInfo==null){
                boolean isAdd=this.dataSourceInfoService.saveDataSourceInfo(dsim);
                if(isAdd){
                    dBMetaDataConfig.refreshDataSource(dsim);
                }
                result.put("status",isAdd);
                result.put("msg","success");
            }else {
                result.put("status",false);
                result.put("msg","数据源名称重复");
            }
        }catch (Exception e){
            log.error("add exception",e);
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }

    @RequestMapping( value = "update",method = RequestMethod.POST)
    @ResponseBody
    public Object update(
        @RequestParam(value = "dataSourceName",required = true) String dataSourceName,
        @RequestParam(value = "jdbcurl",required = true) String jdbcurl,
        @RequestParam(value = "username",required = true) String username,
        @RequestParam(value = "password",required = true) String password,
        @RequestParam(value = "dataSourceId",required = true) String dataSourceId
    ){
        Map<String, Object> result=new HashMap<>();
        try{
            DataSourceInfoModel dsim=new DataSourceInfoModel() ;
            dsim.setDataSourceId(dataSourceId);
            dsim.setDataSourceName(dataSourceName);
            dsim.setUserName(username);
            dsim.setPassword(password);
            dsim.setJdbcUrl(jdbcurl);
            DataSourceInfoModel dsimInfo= this.dataSourceInfoService.findByDataSourceId(dataSourceId);
            boolean isUpdate=false;
            if(dsimInfo!=null){
                if(!dataSourceName.equals(dsimInfo.getDataSourceName())){
                    DataSourceInfoModel dsimInfo2= this.dataSourceInfoService.findByDataSourceName(dataSourceName);
                    if(dsimInfo2!=null){
                        result.put("status",false);
                        result.put("msg","数据源名称重复");
                        return result;
                    }else{
                        isUpdate=this.dataSourceInfoService.updateDataSourceInfo(dsim);
                    }

                }else{
                    isUpdate=this.dataSourceInfoService.updateDataSourceInfo(dsim);
                }
                if(isUpdate){
                    dBMetaDataConfig.refreshDataSource(dsim);
                }
                result.put("status",isUpdate);
                result.put("msg",isUpdate?"success":"修改失败");
            }else{
                result.put("status",false);
                result.put("msg","数据源信息不存在");
            }


        }catch (Exception e){
            log.error("update exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }

    @RequestMapping( value = "deleteById",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteById(
        @RequestParam(value = "id",required = true) int id

    ){
        Map<String, Object> result=new HashMap<>();
        try{


            DataSourceInfoModel dsim= this.dataSourceInfoService.findById(id);
            if(dsim!=null){
             List<ExpRuleModel> expRuleModelList=  this.expRuleService.findByDataSourceId(dsim.getDataSourceId());
             if(expRuleModelList.size()==0){
                boolean flag=this.dataSourceInfoService.deleteDataSourceInfoById(id);
                DBMetaDataConfig.dataSourceMap.remove(dsim.getDataSourceId());
                 result.put("status",flag);
                 result.put("msg","success");
             }else{
                 result.put("status",false);
                 result.put("msg","请先删除关联的导出规则");
             }


            }else{
                result.put("status",false);
                result.put("msg","数据源不存在");
            }

        }catch (Exception e){
            log.error("deleteById exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }
    @RequestMapping( value = "findAll",method = RequestMethod.GET)
    @ResponseBody
    public Object findAll(
    ){
        Map<String, Object> result=new HashMap<>();
        try{

            List<Map<String,Object>> list=this.dataSourceInfoService.findDataSourceInfoByList(null,-1,-1);
            result.put("data",list);
            result.put("status",true);
            result.put("msg","success");
        }catch (Exception e){
            log.error("findAll exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }

    @RequestMapping( value = "findByDataSourceId",method = RequestMethod.GET)
    @ResponseBody
    public Object findAll(
        @RequestParam(value = "dataSourceId",required = true) String  dataSourceId
    ){
        Map<String, Object> result=new HashMap<>();
        try{

            DataSourceInfoModel dsi= this.dataSourceInfoService.findByDataSourceId(dataSourceId);
            result.put("data",dsi);
            result.put("status",true);
            result.put("msg","success");
        }catch (Exception e){
            log.error("findAll exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }

    /**
     * 查询数据字典
     * @param type
     * @return
     */
    @RequestMapping(value = "/findDataDictionary", method = RequestMethod.GET)
    @ResponseBody
    public Object findDataDictionary(
        @RequestParam(value = "type", required = false) String type
    ){
        Map<String, Object> result=new HashMap<>();
        List<DataDictionary> list= this.dataDictionaryService.findByType(type);
        result.put("data", list);
        return result;
    }
}
